package com.sunmouse.toolkit.widget.canvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;


/**
 * Created by Administrator on 2017/10/16 0016.
 */
public class CanvasView extends View {

    private Paint mPaint;
    private Paint mPathPaint;
    private Rect mRect;
    private Path mPath;

    public CanvasView(Context context) {
        this(context, null);
    }

    public CanvasView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.argb(255, 255, 0, 0));
        mPaint.setStyle(Paint.Style.FILL);

        mPathPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPathPaint.setColor(Color.BLACK);
        mPathPaint.setStrokeWidth(5);
        mPathPaint.setStyle(Paint.Style.STROKE);

        mRect = new Rect();
        mPath = new Path();


    }

    /**
     * Notes:
     * _>clipRect and drawPath cannot be used together,or drawPath wil do nothing
     *
     * @param canvas
     */

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mRect.left = getWidth() / 4;
        mRect.top = getHeight() / 4;
        mRect.right = getWidth() / 2 + mRect.left;
        mRect.bottom = getHeight() / 2 + mRect.top;
//        canvas.clipRect(mRect);
        canvas.drawRect(mRect, mPaint);

        /**
         * draw path around rectangle
         */
        mPath.moveTo(mRect.left, mRect.top);
        mPath.lineTo(mRect.right, mRect.top);
        mPath.lineTo(mRect.right, mRect.bottom);
        mPath.lineTo(mRect.left, mRect.bottom);
        mPath.lineTo(mRect.left, mRect.top);
        mPath.close();

        canvas.drawPath(mPath, mPathPaint);
    }
}